var map;
var markersArray = [];
var loaded = 0;
//var infoWindow = new google.maps.InfoWindow;
var toolTipOptions = {
		 content: ''
		,disableAutoPan: false
		,maxWidth: 0
		,pixelOffset: new google.maps.Size(10, -10)
		,zIndex: null
		,boxStyle: { 
		  background: "none"
		  ,opacity: 1
		  ,width: "230px"
		 }
		,closeBoxMargin: "10px 2px 2px 2px"
		,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
		,infoBoxClearance: new google.maps.Size(1, 1)
		,isHidden: false
		,alignBottom: false
		,pane: "floatPane"
		,enableEventPropagation: false
	};
var toolTipWindow = new InfoBox(toolTipOptions);
var contentOptions = {
		 content: ''
		,disableAutoPan: false
		,maxWidth: 0
		,pixelOffset: new google.maps.Size(15, -25)
		,zIndex: null
		,boxStyle: { 
		  background: "none"
		  ,opacity: 1
		  ,width: "230px"
		 }
		,closeBoxMargin: "10px 2px 2px 2px"
		,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
		,infoBoxClearance: new google.maps.Size(1, 1)
		,isHidden: false
		,alignBottom: true
		,pane: "floatPane"
		,enableEventPropagation: false
	};
var contentWindow = new InfoBox(contentOptions);

function initialize() {
	var mapOptions = {
			center: new google.maps.LatLng(initLatitude, initLongitude),
			zoom: 11,
			mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);
	 google.maps.event.addListener(map, 'idle', function(event) {
		 if(loaded == 0){
			 showOverlays(initLatitude, initLongitude, '', '', '', '');
			 loaded = 1;
		 }
     });
}

function addMarker(location) {
	marker = new google.maps.Marker({
		position: location,
		map: map
	});
	markersArray.push(marker);
}

// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
  }
}

function displayOverlay(){
	jQuery('#overlay').css('display', 'block');
}
function closeOverlay(){
	jQuery('#overlay').css('display', 'none');
}

// Shows any overlays currently in the array
function showOverlays(lat, lon, tags, divisions, filter, keyword) {
	toolTipWindow.close();
	contentWindow.close();
	jQuery("#list-view").html('');
	displayOverlay()
	map.setCenter( new google.maps.LatLng(lat, lon));
	clearOverlays();
	markersArray = [];
	jQuery.post(BASE_URL + 'zipcode/getDeals', {lat: lat, lon: lon, tags: tags, divisions: divisions, filter: filter, keyword: keyword}, function(data){
		if(filter == 'search'){
			//dealList = data.ResultSet.Result;
			dealList = data;
			//console.log(dealList);
			listView = '<ul>';
			if(dealList.length > 0){
				for(i = 0; i< dealList.length; i++){
					var latLng = new google.maps.LatLng(dealList[i].Latitude, dealList[i].Longitude);
					var icon = '';
					if(dealList[i].Categories != ''){
						dealCategoryList = dealList[i].Categories.Category;
						var categoryList = eval(localSearchCategories);
						if(dealList[i].Categories.Category.length > 0){
							isExisted = false;
							for(var n = 0; n < dealCategoryList.length; n++){
								for (var k = 0; k < categoryList.length; k++) {
									if(dealCategoryList[n].content == categoryList[k].name){
										icon = categoryList[k].categoryCode;
										isExisted = true;
										break;
									}
								}
								if(isExisted){
									break;
								}
							}
						}
					}
					
					if(icon == ''){
						icon = 'default';
					}
					
					if(icon == 'restaurant'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-03.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
					}else if(icon == 'health-beauty'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-04.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
						
					}else if(icon == 'shopping'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-05.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
					}else if(icon == 'event-attraction'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-06.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
						
					}else if(icon == 'hotel'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-07.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
					}else if(icon == 'food-drug'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-08.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
						
					}else if(icon == 'automotive'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-09.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
					}else if(icon == 'service'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-10.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
						
					}else if(icon == 'home-garden'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-11.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
					}else if(icon == 'medical'){
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-12.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
						
					}else{
						var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-02.png',
					            // This marker is 20 pixels wide by 32 pixels tall.
					            new google.maps.Size(22, 31),
					            // The origin for this image is 0,0.
					            new google.maps.Point(0,0),
					            // The anchor for this image is the base of the flagpole at 0,32.
					            new google.maps.Point(0, 31));
					}
					 var shadow = new google.maps.MarkerImage(BASE_URL + 'media/icons/shadow.png',
					            // The shadow image is larger in the horizontal dimension
					            // while the position and offset are the same as for the main image.
					            new google.maps.Size(37, 31),
					            new google.maps.Point(0,0),
					            new google.maps.Point(0, 31));
					            // Shapes define the clickable region of the icon.
					            // The type defines an HTML &lt;area&gt; element 'poly' which
					            // traces out a polygon as a series of X,Y points. The final
					            // coordinate closes the poly by connecting to the first
					            // coordinate.
			        var shape = {
			            coord: [1, 1, 1, 20, 18, 20, 18 , 1],
			            type: 'poly'
			        };
					var marker = new google.maps.Marker({
						position: latLng,
						title: dealList[i].Title,
						//shadow: shadow,
						//icon: image,
						shape: shape,
						map: map
					});
					marker.setTitle('');
					if(dealList[i].Rating.AverageRating == 'NaN' || dealList[i].Rating.AverageRating == 0){
						averageRating = 0;
					}else{
						averageRating = dealList[i].Rating.AverageRating + ' / 5';
					}
					var tooltip = "<div class='tooltip-content'>" +
										//"<div class='tooltip-image'><img src='' width='60px'/></div>"+
										"<div class='tooltip-name'>"+
										"<h2>" + dealList[i].Title + "</h2>"+
										"			<p>Average Rating: <span>"+averageRating+"</span></p>"+
										"			<p><span class='sale-price'>Total Ratings: "+dealList[i].Rating.TotalRatings+"</span></p>"+
										"			<p>TotalReviews <span>"+dealList[i].Rating.TotalReviews+"</span></p>"+
										"</div>"+
										"<div class='clear'></div>"+
								  "</div>";
					var content = "<div class='deal-info'>" +
										"<div class='deal-title'><a href='"+dealList[i].Url+"' target='_blank'>"+dealList[i].Title+"</a></div>"+
										"<div class='deal-name'><a href='"+dealList[i].Url+"' target='_blank'><span>"+dealList[i].Address+"</span></a></div>"+
										//"<div class='deal-image'><img src=''/></div>"+
										//"<div class='deal-map-links'><a href='https://maps.google.com/maps?hl=en&num=10&ie=UTF-8&cid=0,0,10779485501216626456&fb=1&split=1&gl=us&dq="+encodeURIComponent(dealList[i].business.locations[j].address)+"&daddr="+encodeURIComponent(dealList[i].business.locations[j].address+","+dealList[i].business.locations[j].smart_locality+","+dealList[i].business.locations[j].state+","+dealList[i].business.locations[j].zip_code)+"&geocode="+dealList[i].business.locations[j].lat+","+dealList[i].business.locations[j].lon+"&ei=GGShSq-5HeSf8QawvNDsDw&oi=local_result' target='_blank'>Map directions</a><span>|</span><a href='#' onclick='return openStreetView("+dealList[i].business.locations[j].lat +","+dealList[i].business.locations[j].lon+")'>Street view</a></div>"+
										"<div class='deal-map-links'><a href='#' onclick='return openStreetView("+dealList[i].Latitude +","+dealList[i].Longitude+")'>Street view</a></div>"+
								  "</div>";
				    
				  	//binds the infoWindow to the point
				  	bindInfoWindow(marker, map, toolTipWindow, tooltip, content);
					markersArray.push(marker);
					//For list view
					listView += '<li>';
					listView += '	<div class="list-view-item">';
					listView += '		<div class="list-view-item-left">';
					listView += '			<img src="" height="60px"  />';
					listView += '		</div>';
					listView += '		<div class="list-view-item-middle">';
					listView += '			<h2>'+dealList[i].Title+'</h2>';
					listView += '			<p class="list-view-title"><a href="'+dealList[i].Url+'" target="_blank">'+dealList[i].Title+'</a></p>';
					listView += '			<p class="list-view-expires"><span>Address: '+dealList[i].Address.replace('Suite','<br/>Suite')+'</span></p>';
					//listView += '			<p class="list-view-expires"><span>Expires '+dealList[i].Rating.LastReviewDate+'</span></p>';
					listView += '		</div>';
					listView += '		<div class="list-view-item-right">';
					listView += '			<p class="list-view-deal-from">Total Ratings: <span>'+dealList[i].Rating.TotalRatings+'</span></p>';
					listView += '			<p class="list-view-deal-from"><span class="save-percent">Average Rating: '+averageRating+'</span></p>';
					listView += '			<p class="list-view-deal-from">Toal Reviews <span>'+dealList[i].Rating.TotalReviews+'</span></p>';
					listView += '			<p class="list-view-see-details"><a href="'+dealList[i].Url+'" target="_blank">See details</a></p>';
					listView += '		</div>';
					listView += '		<div class="clear"></div>';
					listView += '	</div>';
					listView += '</li>';
			
				}
				if (markersArray.length > 0) {
					for (var i = 0; i < markersArray.length; i++) {
						markersArray[i].setMap(map);
			        }
				}
				listView += '</ul>';
				jQuery("#list-view").html(listView);
			}else{
				alert('No results found for ' + keyword);
			}
		}else{
			dealList = data.response.deals;
			//alert(dealList.length);
			listView = '<ul>';
			if(dealList.length > 0){
				for(i = 0; i< data.response.deals.length; i++){
					if(dealList[i].business.locations.length > 0){
						for(j = 0; j < dealList[i].business.locations.length; j++){
							var latLng = new google.maps.LatLng(dealList[i].business.locations[j].lat, dealList[i].business.locations[j].lon);
							var icon = '';
							
							if(jQuery('#category-tabs ul li.selected').length > 0){
								icon = jQuery('#category-tabs ul li.selected').attr('id');
							}else{
								var categoryList = eval(categories);
								if(dealList[i].tags.length > 0){
									var tag = dealList[i].tags[0];
									for (var k = 0; k < categoryList.length; k++) {
										if(tag.slug == categoryList[k].slug){
											icon = categoryList[k].categoryCode;
											break;
										}
									}
								}
							}
							
							if(icon == ''){
								icon = 'default';
							}
							
							if(icon == 'restaurant'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-03.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
							}else if(icon == 'health-beauty'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-04.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
								
							}else if(icon == 'shopping'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-05.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
							}else if(icon == 'event-attraction'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-06.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
								
							}else if(icon == 'hotel'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-07.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
							}else if(icon == 'food-drug'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-08.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
								
							}else if(icon == 'automotive'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-09.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
							}else if(icon == 'service'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-10.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
								
							}else if(icon == 'home-garden'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-11.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
							}else if(icon == 'medical'){
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-12.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
								
							}else{
								var image = new google.maps.MarkerImage(BASE_URL + 'media/icons/icon-02.png',
							            // This marker is 20 pixels wide by 32 pixels tall.
							            new google.maps.Size(22, 31),
							            // The origin for this image is 0,0.
							            new google.maps.Point(0,0),
							            // The anchor for this image is the base of the flagpole at 0,32.
							            new google.maps.Point(0, 31));
							}
							 var shadow = new google.maps.MarkerImage(BASE_URL + 'media/icons/shadow.png',
							            // The shadow image is larger in the horizontal dimension
							            // while the position and offset are the same as for the main image.
							            new google.maps.Size(37, 31),
							            new google.maps.Point(0,0),
							            new google.maps.Point(0, 31));
							            // Shapes define the clickable region of the icon.
							            // The type defines an HTML &lt;area&gt; element 'poly' which
							            // traces out a polygon as a series of X,Y points. The final
							            // coordinate closes the poly by connecting to the first
							            // coordinate.
					        var shape = {
					            coord: [1, 1, 1, 20, 18, 20, 18 , 1],
					            type: 'poly'
					        };
							var marker = new google.maps.Marker({
								position: latLng,
								title: dealList[i].business.name,
								//shadow: shadow,
								icon: image,
								shape: shape,
								map: map
							});
							marker.setTitle('');
							
							var tooltip = "<div class='tooltip-content'>" +
												"<div class='tooltip-image'><img src='"+dealList[i].images.image_small+"' width='60px'/></div>"+
												"<div class='tooltip-name'>"+
												"<h2>" + dealList[i].business.name + "</h2>"+
												"			<p>Regular Price: <span>"+dealList[i].value.formatted+"</span></p>"+
												"			<p><span class='sale-price'>Sale Price: "+dealList[i].price.formatted+"</span></p>"+
												"			<p>Save <span>"+dealList[i].discount.formatted+"</span></p>"+
												"</div>"+
												"<div class='clear'></div>"+
										  "</div>";
							var content = "<div class='deal-info'>" +
												"<div class='deal-title'><a href='"+dealList[i].url+"' target='_blank'>"+dealList[i].yipit_title+"</a></div>"+
												"<div class='deal-name'><a href='"+dealList[i].yipit_url+"' target='_blank'><span>"+dealList[i].business.name+"</span></a></div>"+
												"<div class='deal-image'><img src='"+dealList[i].images.image_big+"'/></div>"+
												//"<div class='deal-map-links'><a href='https://maps.google.com/maps?hl=en&num=10&ie=UTF-8&cid=0,0,10779485501216626456&fb=1&split=1&gl=us&dq="+encodeURIComponent(dealList[i].business.locations[j].address)+"&daddr="+encodeURIComponent(dealList[i].business.locations[j].address+","+dealList[i].business.locations[j].smart_locality+","+dealList[i].business.locations[j].state+","+dealList[i].business.locations[j].zip_code)+"&geocode="+dealList[i].business.locations[j].lat+","+dealList[i].business.locations[j].lon+"&ei=GGShSq-5HeSf8QawvNDsDw&oi=local_result' target='_blank'>Map directions</a><span>|</span><a href='#' onclick='return openStreetView("+dealList[i].business.locations[j].lat +","+dealList[i].business.locations[j].lon+")'>Street view</a></div>"+
												"<div class='deal-map-links'><a href='#' onclick='return openStreetView("+dealList[i].business.locations[j].lat +","+dealList[i].business.locations[j].lon+")'>Street view</a></div>"+
										  "</div>";
						    
						  	//binds the infoWindow to the point
						  	bindInfoWindow(marker, map, toolTipWindow, tooltip, content);
							markersArray.push(marker);
							//For list view
							listView += '<li>';
							listView += '	<div class="list-view-item">';
							listView += '		<div class="list-view-item-left">';
							listView += '			<img src="'+dealList[i].images.image_small+'" height="60px"  />';
							listView += '		</div>';
							listView += '		<div class="list-view-item-middle">';
							listView += '			<h2>'+dealList[i].business.name+'</h2>';
							listView += '			<p class="list-view-title"><a href="'+dealList[i].url+'" target="_blank">'+dealList[i].title+'</a></p>';
							listView += '			<p class="list-view-expires"><span>Address: '+dealList[i].business.locations[j].address.replace('Suite','<br/>Suite')+'</span></p>';
							listView += '			<p class="list-view-expires"><span>Expires '+dealList[i].end_date+'</span></p>';
							listView += '		</div>';
							listView += '		<div class="list-view-item-right">';
							listView += '			<p class="list-view-deal-from">Regular Price: <span>'+dealList[i].value.formatted+'</span></p>';
							listView += '			<p class="list-view-deal-from"><span class="save-percent">Sale Price: '+dealList[i].price.formatted+'</span></p>';
							listView += '			<p class="list-view-deal-from">Save <span>'+dealList[i].discount.formatted+'</span></p>';
							listView += '			<p class="list-view-see-details"><a href="'+dealList[i].url+'" target="_blank">See details</a></p>';
							listView += '		</div>';
							listView += '		<div class="clear"></div>';
							listView += '	</div>';
							listView += '</li>';
							
						}
					}
				}
				if (markersArray.length > 0) {
					for (var i = 0; i < markersArray.length; i++) {
						markersArray[i].setMap(map);
			        }
				}
				listView += '</ul>';
				jQuery("#list-view").html(listView);
			}
		}
		closeOverlay();
		
	}, 'json');
}

function openStreetView(lat, lon){
	/*if(index!=null){
		var position = arr_app[index].coupon.latlng;
	}else{
		position = 	new google.maps.LatLng(parseFloat(jJSON["currentIcon"]["lat"]), parseFloat(jJSON["currentIcon"]["lon"]));
	}*/
	var position = 	new google.maps.LatLng(lat, lon);
	var popBox =['<div class="PopupBx" style="z-index:10000;visibility:visible;">',
							  '<div class="PopupHeading">',
								'<div class="PopupHeadingR">',
								  '<div class="PopupHeadingM"> <a href="#" class="close"></a> </div>',
								'</div>',
							  '</div>',
							  '<div class="PopupMiddle">',
								'<div class="PopupMiddleContent">',
								  '<div class="des" style="max-width:99%;width:99%;">',
								  '</div>',
								  '<div class="clr"></div>',
								'</div>',
							  '</div>',
							  '<div class="PopupBottomCO"> </div>',
							  '<div class="PopupBottom">',
								'<div class="PopupBottomR">',
								  '<div class="PopupBottomM">&nbsp;</div>',
								'</div>',
							  '</div>',
						'</div>'].join('');
	
	var p = $(popBox);

	//p.css({top:$('#RightPanel').offset().top+10,left:$('#RightPanel').offset().left+20});
	p.css({top:$('#location-view').offset().top+10,left:$('#location-view').offset().left});
	p.width($("#location-view").width());
	p.find('.des').height(440);
	p.css('position', 'absolute')
	
	p.find('.close').click(function(){
		p.remove();	
		return false;
	});
	
	var sv = new google.maps.StreetViewService();
/*	var panoramaOptions = {
	  position: position,
	  pov: {
		heading: 70,
		pitch: 0,
		zoom: 0
	  }
	};*/
	
	sv.getPanoramaByLocation(position,50,function(data,status){
		if(status==google.maps.StreetViewStatus.OK){
		
			//map.setStreetView(myPano);
			p.appendTo($('body'));
			myPano = new  google.maps.StreetViewPanorama(p.find('.des').get(0));
			
			 myPano.setPano(data.location.pano);
			  myPano.setPov({
				heading: 270,
				pitch: 0,
				zoom: 1
			  });
			 // myPano.setVisible(true);
		}else{
			alert('No street view for this location.');
		}											  
	});
	return false;
}
function bindInfoWindow(marker, map, toolTipWindow, tooltip, content) {
	google.maps.event.addListener(marker, 'click', function() {
		toolTipWindow.close();
		contentWindow.setContent(content);
		contentWindow.open(map, marker);
		//marker.setIcon(orangeStar);
	});
	google.maps.event.addListener(marker, 'mouseover', function() {
		toolTipWindow.setContent(tooltip);
		toolTipWindow.open(map, marker);
		//marker.setIcon(orangeStar);
	});
	google.maps.event.addListener(marker, 'mouseout', function() {
		toolTipWindow.close();
		//marker.setIcon(blueStar);
	});
}

jQuery(window).load(function() {
	initialize();
});
jQuery(document).ready(function() {
	jQuery(function() {
		jQuery( "#localtion-tabs" ).tabs();
		jQuery( "#category-tabs" ).tabs();
	});

	jQuery('#search-zipcode').keypress(function (e){
		if(e.which == 13) {
			keyWord = jQuery('#search-zipcode').val();
			cityValue = jQuery('#city_value').val();
			stateValue = jQuery('#state_value').val();
			urlValue = jQuery('#url_value').val();
			window.location.href = urlValue + encodeURIComponent(keyWord + ' ' + cityValue + ', ' + stateValue);
	    }
		
	});
	jQuery("a.set-homepage").click(function(e){
	    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
	    var bookmarkUrl = this.href;
	    var bookmarkTitle = this.title;
	    if (window.sidebar) { // For Mozilla Firefox Bookmark
	        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
	    } else if(document.all) { // For IE Favorite
	        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
	    } else if(window.opera) { // For Opera Browsers
	    	jQuery("a.set-homepage").attr("href",bookmarkUrl);
	    	jQuery("a.set-homepage").attr("title",bookmarkTitle);
	    	jQuery("a.set-homepage").attr("rel","sidebar");
	    } else { // for other browsers which does not support
	         //alert('Your browser does not support this bookmark action');
	    	if(jQuery('#set-homepage-box').css('display') == 'block'){
	    		jQuery('#set-homepage-box').hide();
	    	}else{
	    		jQuery('#set-homepage-box').show('slow');
	    	}
	         return false;
	    }
	  });
	jQuery('.hide-offscreen').click(function(){
		jQuery('#set-homepage-box').hide();
		return false;
	});
	jQuery('body').click(function(){
		jQuery('#set-homepage-box').hide();
	});
	jQuery('#set-homepage-box').click(function(){
		return false;
	});
	jQuery('#category-tabs ul li').click(function(){
		jQuery('#category-tabs ul li').removeClass('selected');
		jQuery('#category-tabs ul li').find('a').css('font-weight','normal');
		jQuery('#category-tabs ul li').find('a').css('text-decoration','none');
		
		jQuery(this).addClass('selected');
		jQuery(this).find('a').css('font-weight','bold');
		jQuery(this).find('a').css('text-decoration','underline');
	
		category = jQuery(this).attr('id');
		if(category != 'best-deals' && category != 'today-deals'){
			tags = '';
			categoryList = eval(categories);
			
			for (var j = 0; j < categoryList.length; j++) {
				if(category == categoryList[j].categoryCode){
					tags += categoryList[j].slug + ',';
				}
			}
	    
			if(tags == ''){
				toolTipWindow.close();
				contentWindow.close();
				jQuery("#list-view").html('');
				clearOverlays();
				return false;
			}
			
			if(tags != ''){
				tags = tags.substring(0, tags.length - 1);
				
			}
			showOverlays(initLatitude, initLongitude, tags, '', '', '');
		}else{
			showOverlays(initLatitude, initLongitude, '', '', category, '');
		}
		return false;
	});
	jQuery('#submit-search-category').click(function(){
		jQuery('#category-tabs ul li').removeClass('selected');
		jQuery('#category-tabs ul li').find('a').css('font-weight','normal');
		jQuery('#category-tabs ul li').find('a').css('text-decoration','none');
		keywordText = jQuery.trim(jQuery('#keyword').val());
		if(keywordText == ''){
			return false;
		}
		showOverlays(initLatitude, initLongitude, '', '', 'search', keywordText);		
	});
});

function onfocusText(text, obj){
	if(jQuery(obj).val() == text){
		jQuery(obj).val('');
	}
}

function onblurText(text, obj){
	if(jQuery(obj).val() == ''){
		jQuery(obj).val(text);
	}
}